<?php

namespace app\common\lib;

use think\facade\Log;

class toCamelCase
{
    public function toCamelCaseArray($array,$flag = 1,$type = 1) {
        $result = [];
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                // 如果值是数组，递归处理
                $result[$this->camelize($key,$type)] = $this->toCamelCaseArray($value);
            } else {
                // 转换字段名为驼峰命名
                $result[$this->camelize($key,$type)] = $value;
            }
        }
        if($flag == 2 && count($array) == 1){
            return $result[0];
        }else{
            return $result;
        }
    }

    public function camelize($string,$type = 1) {
        if($type == 1){
            return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $string))));
        }else{
            return strtolower(preg_replace('/([a-z\d])([A-Z])/', "$1_$2", $string));
        }
    }

    public function camelToSnake($str) {
        // 使用正则表达式，确保不会匹配字符串的开始位置
        return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $str));
    }
}
